package com.itheima.health.controller;

import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.UploadUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
public class SetMealController {

    @Reference
    SetmealService setmealService;

    @RequestMapping("setmeal/upload")
    public Result upload(@RequestParam("imgFile")MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        String fileName = UploadUtils.generateRandonFileName(originalFilename);
        try {
            AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(),fileName);
            RedisUtil.addToSet(RedisConstant.ALL_SETMEAL_PIC_SET,fileName);
            RedisUtil.set(RedisConstant.SINGLE_PIC+fileName,fileName,10, TimeUnit.MINUTES);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(fileName);
    }

    @PostMapping("setmeal/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        return new Result(setmealService.findPage(queryPageBean));
    }

    @PostMapping("setmeal/addSetMeal")
    public Result addSetMeal(@RequestBody SetmealDTO setmealDTO){
        Boolean flag = setmealService.saveSetMeal(setmealDTO);
        RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,setmealDTO.getImg());
        RedisUtil.removeKeys(RedisConstant.SINGLE_PIC+setmealDTO.getImg());
        return new Result(flag);
    }

    /*@GetMapping("checkgroup/findCheckItemInfoByGroupId/{id}")
    public Result findCheckItemIdsByGroupId(@PathVariable("id") int id){
        return new Result(checkGroupService.findCheckItemInfoByGroupId(id));
    }*/

    /*@DeleteMapping("checkgroup/deleteCheckGroup/{id}")
    public Result deleteCheckGroup(@PathVariable("id") int id){
        return new Result(checkGroupService.deleteCheckGroup(id));
    }*/
}
